package com.xsm.activitygroup;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sina.weibo.sdk.api.share.VersionCheckHandler;
import com.umeng.analytics.MobclickAgent;
import com.xsm.DataBase.SaveMessage;
import com.xsm.activitygroup.R;
import com.xsm.function.AskForResult;
import com.xsm.internet.PostMessage;
import com.xsm.object.UpdateObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.TextView;
//----sharedPreferences的参数------
//IsLogin是否已经登陆
//versionname版本名
//packageName包名
//versioncode版本号
//--------表版本----------
//authorv现在的作者库版本
//pressv现在的出版社版本





public class Welcome extends Activity{
	private	String IsLogin;
	private	Context context;
	private	TextView versionname;
	private  String result="";
    private  MyHandler handler=new MyHandler();
	private SharedPreferences sharedPreferences;
protected void onCreate(Bundle savedInstanceState) {
	requestWindowFeature(Window.FEATURE_NO_TITLE);
	super.onCreate(savedInstanceState);
	setContentView(R.layout.welcome);
	versionname=(TextView)findViewById(R.id.versionname);
	context=this;
	// 获取packagemanager的实例
    PackageManager packageManager = getPackageManager();
    // getPackageName()是你当前类的包名，0代表是获取版本信息
    PackageInfo packInfo;
    String version="";
	try {
		packInfo = packageManager.getPackageInfo(getPackageName(),0);
		 version = packInfo.versionName;	 
	} catch (NameNotFoundException e) {
	}
    sharedPreferences=getSharedPreferences("XSM",0);
    IsLogin=sharedPreferences.getString("IsLogin","");
	versionname.setText("v "+version); 
    sharedPreferences.edit().putString("versionname",version).commit();
	new Thread(new Runnable() {
public void run() {
UpdateObject object=new AskForResult().getTableVersion();
sharedPreferences.edit().putString("nauthorv", object.getAuthorv()).commit();
sharedPreferences.edit().putString("npressv",object.getPressv()).commit();
sharedPreferences.edit().putString("nbooknamev",object.getBooknamev()).commit();
sharedPreferences.edit().putString("nrecommendv",object.getRecommendv()).commit();
sharedPreferences.edit().putString("ntextbookav",object.getTextbookav()).commit();
sharedPreferences.edit().putString("ntextbookbv",object.getTextbookbv()).commit();
sharedPreferences.edit().putString("ntextbookcv",object.getTextbookcv()).commit();

Message msg=handler.obtainMessage();		
if(IsLogin.equals("")){
	msg.what=2;
}else {
	msg.what=3;
}
handler.sendMessage(msg);
		}
	}).start();
}


class MyHandler extends Handler{
	public void handleMessage(Message msg) {
      if(msg.what==2){
		startActivity(new Intent(Welcome.this,Login.class));		
		}else if(msg.what==3){
		startActivity(new Intent(Welcome.this,MainActivity.class));	
		}
      finish();
	}

}

public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==event.KEYCODE_BACK){
}
	return true;
}

@Override
protected void onResume() {
	MobclickAgent.onResume(this);
	super.onResume();
}
@Override
protected void onPause() {
	MobclickAgent.onResume(this);
	super.onPause();
}

public String getversion(String name){
	List params=new ArrayList();
	params.add(new BasicNameValuePair("ASK","GetVersion"));
	params.add(new BasicNameValuePair("name",name));
	PostMessage postMessage=new PostMessage(params);
	String result=postMessage.getResult();
	return result;
}

}
